//
//  TransactionViewController.swift
//  YUANSHI
//
//  Created by pro on 2019/12/7.
//  Copyright © 2019 wangbin. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON
class TransactionViewController: WBBaseViewController {

    var eventInfos:[tranHomeModel]?
    private let tranCellID = "TranTableViewCell"
    lazy var tableView : UITableView = {
        
        let tb  = UITableView.init(frame: .zero, style: .grouped)
        tb.delegate = self
        tb.dataSource = self
        tb.uHead = URefreshHeader{ [weak self] in self?.getDataToRelodTabelView() }
        tb.backgroundColor = UIColor.red
        tb.register(TranTableViewCell.self, forCellReuseIdentifier: tranCellID)
        return tb
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

//        getDataToRelodTabelView()
        self.tableView.uHead.beginRefreshing()
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalToSuperview()
        }

//        getDataToRelodTabelView()

        
    }


    func getDataToRelodTabelView(){
        
               // 1. 获取json文件路径
        let path = Bundle.main.path(forResource: "FindAttention", ofType: "json")
        // 2. 获取json文件里面的内容,NSData格式
        let jsonData=NSData(contentsOfFile: path!)
        // 3. 解析json内容
        let json = JSON(jsonData!)
        if let mappedObject = JSONDeserializer<tranHomeModel>.deserializeModelArrayFrom(json: json["data"]["eventInfos"].description) {
            self.eventInfos = mappedObject as? [tranHomeModel]
        }
        
        if self.eventInfos?.count ?? 0 > 0 {
            self.tableView.uHead.endRefreshing()
            self.tableView.reloadData()
        }
        print(self.eventInfos!);
    }


}

extension TransactionViewController : UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.eventInfos?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
//       let cell = UITableViewCell.init(style: .default, reuseIdentifier: "UITableViewCell")
//        cell.textLabel?.text = String(format: "哈哈哈%d",indexPath.row )
////        cell.selectionStyle = UITableViewCellSelectionStyleNone
//
        
        let cell = tableView.dequeueReusableCell(withIdentifier: tranCellID, for: indexPath) as! TranTableViewCell
        
        cell.selectionStyle = .none
        cell.tranModel = self.eventInfos?[indexPath.row]
        cell.picModel = self.eventInfos?[indexPath.row].contentInfo?.picInfos?[0]
        return cell
        
//        let cell = tableView.dequeueReusableCell(withIdentifier: tranCellID, for: indexPath)
//        cell.textLabel?.text = "hello here"
//        return cell
        
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView.init()
        footerView.backgroundColor = UIColor.white
        return footerView
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView.init()
        headerView.backgroundColor = UIColor.yellow
        return headerView
    }
    
    
}
